package com.shsxt.jdbc;

import java.beans.PropertyVetoException;
import java.sql.Connection;
import java.sql.SQLException;

import org.apache.log4j.Logger;

import com.mchange.v2.c3p0.ComboPooledDataSource;
import com.shsxt.jdbc.constant.DBconConstant;
import com.shsxt.util.CfgReadUtil;

/**
 * 使用C3P0连接池获取数据库连接对象 
 * @author Mr.YongGan.Zhang
 * @version 1.0.0
 * 
 */
public final class MDBManager {
	
	  //	日志
	private static final  Logger  LOGGER  = Logger.getLogger(MDBManager.class);
	
	private static final MDBManager instance = new MDBManager();  
	
    private static ComboPooledDataSource cpds=new ComboPooledDataSource(true);
    
    static {
    	// 连接的url
        cpds.setJdbcUrl(CfgReadUtil.get(DBconConstant.YONGGAN_JDBCURL));  
        try {
			cpds.setDriverClass(CfgReadUtil.get(DBconConstant.YONGGAN_DRIVERCLASS));
		} catch (PropertyVetoException e) {
			LOGGER.error(" DriverClass  load  fail " ,e);
		}
        // 基本用户信息
        cpds.setUser(CfgReadUtil.get(DBconConstant.YONGGAN_USER));
        cpds.setPassword(CfgReadUtil.get(DBconConstant.YONGGAN_PASSWORD));
        cpds.setMinPoolSize(Integer.valueOf(CfgReadUtil.get(DBconConstant.YONGGAN_MINPOOLSIZE)));
        cpds.setMaxPoolSize(Integer.valueOf(CfgReadUtil.get(DBconConstant.YONGGAN_MAXPOOLSIZE)));
        cpds.setMaxIdleTime(Integer.valueOf(CfgReadUtil.get(DBconConstant.YONGGAN_MAXIDLETIME)));
        cpds.setInitialPoolSize(Integer.valueOf(CfgReadUtil.get(DBconConstant.YONGGAN_INITIALPOOLSIZE)));
        cpds.setAcquireIncrement(Integer.valueOf(CfgReadUtil.get(DBconConstant.YONGGAN_ACQUIREINCREMENT)));
    }
    
    public static MDBManager getInstance(){  
        return instance;  
    }  
    
    public static Connection  getConnection(){  
        try {  
            return cpds.getConnection();  
        } catch (SQLException e) {  
            e.printStackTrace();  
        }  
        return null;  
    }
    
    public static void main(String[] args) {
		System.out.println(getConnection());
	}

}
